package com.maduar.demo.algo.interview;

import io.swagger.models.auth.In;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Description:
 *
 * @author maduar maduar@163.com
 * @date 2021-03-11 21:35
 */
public class RandomArray {

    /**
     * 1. 接口名称: 随机输出list的数据
     * 2. 接口文档编号:
     * 3. 接口详细说明:
     *
     * @param list 需要打乱顺序的list
     * @return List
     * @Author: danny.pang
     * @Date: 2021-03-11 22:12:05
     */
    public static List<Integer> randomList(List<Integer> list) {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            int randomNumber = (int) (Math.random() * size);
            int swapNumber = list.get(randomNumber);
            int current = list.get(i);
            list.set(i, swapNumber);
            list.set(randomNumber, current);
        }

        return list;
    }

    /**
     * 1. 接口名称: 随机输出数组中的数据
     * 2. 接口文档编号:
     * 3. 接口详细说明:
     *
     * @param arr 需要打乱顺序的数组
     * @return int[] 打乱顺序后的数组
     * @Author: danny.pang
     * @Date: 2021-03-11 22:12:48
     */
    public static int[] randomArray(int[] arr) {
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            int randomNumber = (int) (Math.random() * length);
            int swapNumber = arr[randomNumber];
            int current = arr[i];

            arr[i] = swapNumber;
            arr[randomNumber] = current;
        }

        return arr;
    }
}
